package com.foreveross.fetl.application.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.ITaskTemplateApplication;
import com.foreveross.fetl.common.application.BaseApplicationImpl;
import com.foreveross.fetl.core.domain.task.TaskTemplateAssignDayEntity;
import com.foreveross.fetl.core.domain.task.TaskTemplateEntity;
import com.foreveross.fetl.vo.TaskTemplateVO;

@Transactional
@Service("taskTemplateApplication")
public class TaskTemplateApplicationImpl extends BaseApplicationImpl implements
		ITaskTemplateApplication {

	@Override
	public void save(TaskTemplateVO vo) {
		TaskTemplateEntity entity = new TaskTemplateEntity();
		entity.setId(vo.getId());
		entity.setAreaCode(vo.getAreaCode());
		entity.setAreaName(vo.getAreaName());
		entity.setAttachHbaseKey(vo.getAttachHbaseKey());
		entity.setFrequency(vo.getFrequency());
		entity.setFromCity(vo.getFromCity());
		entity.setFromCityName(vo.getFromCityName());
		entity.setToCity(vo.getToCity());
		entity.setToCityName(vo.getToCityName());
		entity.setGrabChannel(vo.getGrabChannel());
		entity.setGrabChannelId(vo.getGrabChannelId());
		entity.setGrabScopeBegin(vo.getGrabScopeBegin());
		entity.setGrabScopeEnd(vo.getGrabScopeEnd());
		entity.setIsInternational(vo.getIsInternational());
		entity.setIsReturn(vo.getIsReturn());
		entity.setName(vo.getName());
		entity.setPriority(vo.getPriority());
		entity.setRemark(vo.getRemark());
		entity.setReturnDateScope(vo.getReturnDateScope());
		entity.setStatus(vo.getStatus());
		entity.setUserId(vo.getUserId());
		entity.setUserName(vo.getUserName());
		entity.setValidityFrom(vo.getValidityFrom1());
		entity.setValidityTo(vo.getValidityTo1());
		entity.setNextGrabTime(vo.getNextGrabTime1());
		entity.saveOrUpdate(entity);
	}

	@Override
	public void delete(TaskTemplateVO vo) {
		TaskTemplateEntity entity = new TaskTemplateEntity();
		TaskTemplateAssignDayEntity entity1 = new TaskTemplateAssignDayEntity();
		entity1.getTaskTemplateAssignDayRepository()
				.createQuery(
						"delete TaskTemplateAssignDayEntity ttade where ttade.templateId= ?",
						new Long[] { vo.getId() }).executeUpdate();
		entity.getTaskTemplateRepository()
				.createQuery("delete TaskTemplateEntity tte where tte.id= ?",
						new Long[] { vo.getId() }).executeUpdate();
	}

	@Override
	public TaskTemplateVO get(Long id) {
		TaskTemplateVO vo = new TaskTemplateVO();
		TaskTemplateEntity entity = new TaskTemplateEntity();
		try {
			BeanUtils.copyProperties(vo, entity.get(id));
			return vo;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public TaskTemplateVO find(String channel, String fromCity, String toCity) {
		List<TaskTemplateEntity> listEntity = new ArrayList<TaskTemplateEntity>();
		TaskTemplateEntity entity = new TaskTemplateEntity();
		listEntity = entity.findByCondition(channel, fromCity, toCity);
		TaskTemplateVO vo = null;
		if (null != listEntity && listEntity.size() > 0) {
			vo = new TaskTemplateVO();
			try {
				BeanUtils.copyProperties(vo, listEntity.get(0));
				vo.setId(listEntity.get(0).getId());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return vo;
	}

	@Override
	public Page<TaskTemplateVO> pageQuery(String channel, String fromCity,
			String toCity, int currentPage, int pageSize) {
		List<TaskTemplateVO> result = new ArrayList<TaskTemplateVO>();
		StringBuffer jpql = new StringBuffer(
				"select tte from TaskTemplateEntity tte where 1=1");
		List<Object> conditionVals = new ArrayList<Object>();
		if (channel != null && !channel.isEmpty()) {
			jpql.append(" and tte.attachHbaseKey =?");
			conditionVals.add(channel);
		}
		if (fromCity != null && !fromCity.isEmpty()) {
			jpql.append(" and tte.fromCity =?");
			conditionVals.add(fromCity);
		}
		if (toCity != null && !toCity.isEmpty()) {
			jpql.append(" and tte.toCity =?");
			conditionVals.add(toCity);
		}
		jpql.append(" order by tte.attachHbaseKey desc");
		Page<TaskTemplateEntity> pages = this.queryChannel1()
				.queryPagedResultByPageNo(jpql.toString(),
						conditionVals.toArray(), currentPage, pageSize);
		for (TaskTemplateEntity en : pages.getResult()) {
			TaskTemplateVO vo = new TaskTemplateVO();
			try {
				BeanUtils.copyProperties(vo, en);
				vo.setId(en.getId());
				result.add(vo);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return new Page<TaskTemplateVO>(pages.getCurrentPageNo(),
				pages.getTotalCount(), pages.getPageSize(), result);
	}

}
